package com.edu.zju.scs.panacea.service.resource;

import com.edu.zju.scs.panacea.entity.BaseResult;
import com.edu.zju.scs.panacea.entity.resource.Loc;
import com.edu.zju.scs.panacea.entity.resource.Package;
import com.edu.zju.scs.panacea.entity.vo.AvalibleLocsVO;
import com.edu.zju.scs.panacea.entity.vo.PackageVO;
import com.edu.zju.scs.panacea.mapper.plan.TaskMapper;
import com.edu.zju.scs.panacea.mapper.resource.LocMapper;
import com.edu.zju.scs.panacea.mapper.resource.PackageMapper;
import com.edu.zju.scs.panacea.mapper.resource.PackageRelLocMapper;
import com.edu.zju.scs.panacea.mapper.resource.TemplateRelLocMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * @Author: Yue Liang
 * @Date: 2018-12-27 17:52
 */
@Service
public class PackageService {

    @Autowired
    private PackageMapper packageMapper;

    @Autowired
    private PackageRelLocMapper packageRelLocMapper;

    @Autowired
    private TemplateRelLocMapper templateRelLocMapper;

    /**
     * 根据商家ID查询其下所有的资源包
     * @param sellerId
     * @return
     */
    public BaseResult getPackagesBySeller(Long sellerId) {
        BaseResult baseResult = new BaseResult();
        List<Package> packageList = packageMapper.selectPackageBySeller(sellerId);
        List<PackageVO> packageVOList = new ArrayList<>();
        for (Package resPackage : packageList) {
            PackageVO packageVO = new PackageVO();
            packageVO.setId(resPackage.getId());
            packageVO.setName(resPackage.getName());
            packageVO.setState(resPackage.getState());
            List<Long> locIds = packageRelLocMapper.selectLocsByPackage(resPackage.getId());
            packageVO.setLocsNum(locIds.size());
            packageVOList.add(packageVO);
        }
        if (packageList == null) {
            baseResult.error("查询失败！");
        } else {
            baseResult.success(packageVOList);
        }
        return baseResult;
    }

    /**
     * 新增资源包
     * @param resPackage
     * @return
     */
    public BaseResult addPackage(Package resPackage) {
        BaseResult baseResult = new BaseResult();
        packageMapper.insertPackage(resPackage);
        Long newPackageId = resPackage.getId();
        if (newPackageId == null) {
            baseResult.error("新增资源包失败！");
        } else {
            packageRelLocMapper.insertPackageRelLoc(resPackage.getId(), resPackage.getLocIds());
            baseResult.success(resPackage.getId());
        }
        return baseResult;
    }


    /**
     * 删除资源包以及其下所有的资源位
     * @param id
     * @return
     */
    public BaseResult deletePackage(Long id) {
        BaseResult baseResult = new BaseResult();
        Package resPackage = packageMapper.selectPackage(id);
        if (resPackage == null) {
            baseResult.error("资源包不存在");
            return baseResult;
        }

        if (resPackage.getState() != 0) {
            baseResult.error("正在使用中的资源包不可删除");
            return baseResult;
        }

        long deleteNums = packageMapper.deletePackage(id);
        packageRelLocMapper.deletePackageRelLoc(id);
        if (deleteNums == 0) {
            baseResult.error("删除失败！");
        } else {
            baseResult.success(deleteNums);
        }
        return baseResult;
    }


    /**
     * 查询指定资源包下，可用资源位的个数，资源位需去重
     * @param
     * @return
     */
    public BaseResult getAvalibleLocs(List<Long> packageIds, List<Long> locIds, Integer type) {
        BaseResult baseResult = new BaseResult();
        Set<Long> set = new HashSet<>();
        if (packageIds != null) {
            for (Long packageId : packageIds) {
                List<Long> locList = packageRelLocMapper.selectLocsByPackage(packageId);
                for (Long locId : locList) {
                    set.add(locId);
                }
            }
        }

        if (locIds != null) {
            for (Long locId : locIds) {
                set.add(locId);
            }
        }

        Set<Long> typeFilterSet = templateRelLocMapper.selectLocByTemplate(set, type);
        AvalibleLocsVO avalibleLocsVO = new AvalibleLocsVO();
        avalibleLocsVO.setLocs(typeFilterSet);
        avalibleLocsVO.setNum(typeFilterSet.size());
        baseResult.success(avalibleLocsVO);
        return baseResult;
    }
}
